<template>
    <div class="FinancialInfo" style="width: 100%;">
        <template v-show="hasTotal">
            <rui-form :form-options="formOptions3" :label-width="120" :read-only="true"
                      ref="formFinancialInfo3">
                <template slot="typeSlot" slot-scope="{ key,data,dataDic }">
                    <a @click="openModal">{{getTitleFromDataDict(dataDic,data[key])}}</a>
                </template>
                <template slot="isLpr" slot-scope="{ key,data,dataDic }">
                    <span v-if="data[key]==='0'">固定</span>
                    <span v-else>浮动</span>
                </template>
            </rui-form>
        </template>
        <Modal v-model="financialInfoModal" title="金融信息 " :width="800" footer-hide>
            <List>
                <ListItem v-show="hasCarAmt">
                    <rui-form :form-options="formOptions" :label-width="120" :read-only="true"
                              ref="formFinancialInfo">
                        <template slot="isLpr" slot-scope="{ key,data,dataDic }">
                            <span v-if="data[key]==='0'">固定</span>
                            <span v-else>浮动</span>
                        </template>
                        <template slot="downPayScale" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="custRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="settleRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="discountRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                    </rui-form>
                </ListItem>
                <ListItem v-show="hasAdditional">
                    <rui-form :form-options="formOptions2" :label-width="120" :read-only="true"
                              ref="formFinancialInfo2">
                        <template slot="isLpr" slot-scope="{ key,data,dataDic }">
                            <span v-if="data[key]==='0'">固定</span>
                            <span v-else>浮动</span>
                        </template>
                        <template slot="downPayScale" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="custRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="settleRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                        <template slot="discountRate" slot-scope="{ key,data }">
                            <span v-if="data[key]">{{data[key]}}%</span>
                        </template>
                    </rui-form>
                </ListItem>
            </List>
        </Modal>
    </div>
</template>

<script>
    import {deepClone} from "@/libs/utils/ObjectClone";
    import {getTitleFromDataDict} from "_p/basic/assets/contract/js/utils"

    export default {
        props: {
            contractNo: {
                type: String,
                required: true
            }
        },
        data() {
            return {
                financialInfoModal: false,
                // CAR_AMT, ADDITIONAL, TOTAL;
                hasCarAmt: false,
                hasAdditional: false,
                hasTotal: false,
                defines: [],
                formFinancialInfoType: "",
                formOptions: [
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["type", "loanAmt", "isLpr"],
                                slots: [{key: 'isLpr', slotName: 'isLpr'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: [
                                    "downPayAmt",
                                    "settleRate",
                                    "financPercent"
                                ],
                                slots: [{key: 'settleRate', slotName: 'settleRate'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: [
                                    "downPayScale",
                                    "contractAmt1",
                                    "productName"
                                ],
                                slots: [{key: 'downPayScale', slotName: 'downPayScale'}]
                            }
                        ]
                    },
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["monthPayType", "monthPayAmt"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["discountRate", "tailPayAmt"],
                                slots: [{key: 'discountRate', slotName: 'discountRate'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["custRate", "countTerm1"],
                                slots: [{key: 'custRate', slotName: 'custRate'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["repaymentType"]
                            }
                        ]
                    }
                ],
                formOptions2: [
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["type", "loanAmt"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["downPayAmt", "settleRate"],
                                slots: [{key: 'settleRate', slotName: 'settleRate'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["downPayScale", "productName"],
                                slots: [{key: 'downPayScale', slotName: 'downPayScale'}]
                            }
                        ]
                    },
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["monthPayType", "monthPayAmt", "isLpr"],
                                slots: [{key: 'isLpr', slotName: 'isLpr'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["discountRate", "tailPayAmt","repaymentType"],
                                slots: [{key: 'discountRate', slotName: 'discountRate'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["custRate", "financPercent","countTerm1"],
                                slots: [{key: 'custRate', slotName: 'custRate'}]
                            },
                        ]
                    }
                ],
                formOptions3: [
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["type", "loanAmt", "isLpr"],
                                slots: [{key: 'type', slotName: 'typeSlot'}, {key: 'isLpr', slotName: 'isLpr'}]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["downPayAmt", "contAmt", "tailPayAmt"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["contractAmt", "productName", "monthPayAmt"]
                            }
                        ]
                    },
                    {
                        isGroup: false,
                        grids: [
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["monthPayType"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["difference"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["overpayment"]
                            },
                            {
                                defineId: "defineData",
                                span: 8,
                                fields: ["repaymentType"]
                            }
                        ]
                    }
                ]
            };
        },
        computed: {
            getTitleFromDataDict: () => getTitleFromDataDict
        },
        mounted() {
        },
        methods: {
            openModal() {
                this.financialInfoModal = true
            },
            updateData(data) {
                console.log("FinancialInfo===updateData", data);
                if (!data && !Array.isArray(data)) return;
                data.forEach(ele => {
                    this.getType(ele);
                });
                // 车款 附加贷 汇总
                // let CAR_AMT, ADDITIONAL, TOTAL;

            },

            getType(data) {
                if (!data && !data.type) return;
                let d = deepClone(data);
                switch (data.type) {
                    case "CAR_AMT":
                        d.contractAmt1 = d.contractAmt;
                        this.$refs.formFinancialInfo.updateFormData(d);
                        if (data.overpayment === 0) {
                            this.$refs.formFinancialInfo.setFiledValue('overpayment', '0')
                        }
                        if (data.settleRate === 0) {
                            this.$refs.formFinancialInfo.setFiledValue('settleRate', '0')
                        }
                        if (data.tailPayAmt === 0) {
                            this.$refs.formFinancialInfo.setFiledValue('tailPayAmt', '0')
                        }
                        if (data.discountRate === 0) {
                            this.$refs.formFinancialInfo.setFiledValue('discountRate', '0')
                        }
                        this.hasCarAmt = true;

                        break;
                    case "ADDITIONAL":
                        this.$refs.formFinancialInfo2.updateFormData(data);
                        if (data.tailPayAmt === 0) {
                            this.$refs.formFinancialInfo2.setFiledValue('tailPayAmt', '0')
                        }
                        if (data.contractAmt === undefined) {
                            this.$refs.formFinancialInfo2.setFiledValue('contractAmt', '0')
                        }
                        this.hasAdditional = true;
                        break;
                    case "TOTAL":
                        d.contAmt = d.contractAmt;
                        d.contractAmt = d.addAmt;

                        this.$refs.formFinancialInfo3.updateFormData(d);

                        if (data.overpayment === 0) {
                            this.$refs.formFinancialInfo3.setFiledValue('overpayment', '0')
                        }
                        if (data.contractAmt === undefined) {
                            this.$refs.formFinancialInfo3.setFiledValue('contractAmt', '0')
                        }
                        if (data.difference === 0) {
                            this.$refs.formFinancialInfo3.setFiledValue('difference', '0')
                        }
                        if (data.tailPayAmt === 0) {
                            this.$refs.formFinancialInfo3.setFiledValue('tailPayAmt', '0')
                        }
                        if (data.custRate === 0) {
                            this.$refs.formFinancialInfo3.setFiledValue('custRate', '0')
                        }
                        if (data.tailPayAmt === 0) {
                            this.$refs.formFinancialInfo3.setFiledValue('tailPayAmt', '0')
                        }
                        this.hasTotal = true;
                        break;
                }
            }

        }
    };
</script>
